//
//  SecondViewController.m
//  WorkingResult
//
//  Created by 河野 智遵 on 11/08/14.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "TaskListViewController.h"
#import "TaskEditViewController.h"
#import "Task.h"

@implementation TaskListViewController

@synthesize model;
@synthesize moveToAddingTaskEditorActionTarget;
@synthesize moveToAddingTaskEditorAction;
@synthesize moveToUpdatingTaskEditorActionTarget;
@synthesize moveToUpdatingTaskEditorAction;

- (void) setLeftButtonItem
{
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onAdd)] animated:YES];
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self setLeftButtonItem];
    [self setEditingList:NO];
    selectedTaskIndex_ = -1;
}

- (void) setEditingList:(BOOL)editingList
{
    [self setEditing:editingList animated:YES];
    UIBarButtonItem* item ;
    if (editingList)
    {
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onDone)];
    }
    else
    {
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(onEdit)];
    }
    [self.navigationItem setRightBarButtonItem:item animated:YES];
}

-(void)onAdd
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    [self setEditingList:NO];
    [self.moveToAddingTaskEditorActionTarget performSelector:self.moveToAddingTaskEditorAction];
}

- (void)onDone
{
    [self setLeftButtonItem   ];
    [self setEditingList:NO];
}

- (void)onEdit
{
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    [self setEditingList:YES];
}

-(id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self != nil)
    {
        self.title = @"タスク一覧";
    }
    return self;
}

-(NSInteger)getRowCount 
{
    return [model getTaskCount];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self getRowCount];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [model deleteAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSInteger labelTag = 100;
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 26)];
        label.center = CGPointMake(cell.bounds.size.width - 80, cell.center.y);
        label.textAlignment = UITextAlignmentCenter;
        label.layer.cornerRadius = 6;

        Task* task = [model taskAtIndex:indexPath.row];
        cell.textLabel.text = task.text;
        label.backgroundColor = task.color;
        label.tag = labelTag;

        [cell addSubview:label];
    }
    else
    {
        Task* task = [model taskAtIndex:indexPath.row];
        cell.textLabel.text = task.text;
        UILabel* label = (UILabel*)[cell viewWithTag:labelTag];
        label.backgroundColor = task.color;
    }
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedTaskIndex_ = indexPath.row;
    [self.moveToUpdatingTaskEditorActionTarget performSelector:self.moveToUpdatingTaskEditorAction];
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

- (NSInteger) getSelectedTaskIndex_;
{
    return selectedTaskIndex_;
}

@end
